package com.lungle.warmdiary.app.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.utils.TDevice;

/**
 * Created by God on 2016/9/6.
 */
public class ImagePreview {


    private PopupWindow mImagePreviewPop;
    private ProgressBar mPbLoading;
    private Activity mContext;
    private LayoutInflater mInflater;
    private ImageView mIvPreview;

    public ImagePreview(Activity context) {
        mContext = context;
        initPicturePreview(mContext.getWindow().getDecorView());
    }

    private void initPicturePreview(View root) {
        if (mImagePreviewPop == null) {
            mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = mInflater.inflate(R.layout.view_image_preview, null);
            mPbLoading = (ProgressBar) view.findViewById(R.id.pb_loading);
            mIvPreview = (ImageView) view.findViewById(R.id.iv_preview);
            mIvPreview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mImagePreviewPop.dismiss();
                    hidePreview();
                }
            });


            mImagePreviewPop = new PopupWindow(view, (int) (TDevice.getScreenWidth() / 5) * 4, (int) (TDevice.getScreenHeight() / 5) * 4);

            mImagePreviewPop.setBackgroundDrawable(new BitmapDrawable());
            mImagePreviewPop.update();
            mImagePreviewPop.setFocusable(false);
            mImagePreviewPop.setOutsideTouchable(true);
            mImagePreviewPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    hidePreview();
                    WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
                    lp.alpha = 1f;
                    mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                    mContext.getWindow().setAttributes(lp);
                }
            });
//            mSelectActive.setBackgroundDrawable(new BitmapDrawable());


        }
    }


    public void showPreview(View view, String url) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.4f;
        mPbLoading.setVisibility(View.VISIBLE);
        mIvPreview.setVisibility(View.INVISIBLE);
        mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mContext.getWindow().setAttributes(lp);
        mImagePreviewPop.showAtLocation(view, Gravity.CENTER, 0, 0);
        mPbLoading.setVisibility(View.VISIBLE);
        ImageLoader.getInstance().loadImage(url, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                mPbLoading.setVisibility(View.GONE);
                mIvPreview.setVisibility(View.VISIBLE);
                mIvPreview.setImageBitmap(loadedImage);

            }
        });
    }

    public void hidePreview() {
        ImageLoader.getInstance().stop();
        mPbLoading.setVisibility(View.GONE);
    }


}
